package cn.btks.resultintegration.achievements.controller;

import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.result.SystemLogEnum;
import cn.btks.commonserver.valid.SystemLog;
import cn.btks.resultintegration.achievements.dto.AchievementQueryParamDto;
import cn.btks.resultintegration.achievements.pojo.Achievements;
import cn.btks.resultintegration.achievements.service.AchievementsService;
import cn.btks.resultintegration.achievements.service.IServerService;
import cn.btks.resultintegration.catalog.dto.CatalogDto;
import cn.btks.resultintegration.catalog.service.CatalogService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author lijun
 * @Date 2020/12/16 17:05
 */
@Slf4j
@RestController
@RequestMapping("api/v1.0/resultIntegration/achievements")
@Api(tags = "成果整合模块")
@Validated
public class AchievementsController {

    @Autowired
    IServerService iServerService;

    @Autowired
    AchievementsService achievementsService;

    @Autowired
    CatalogService catalogService;

    @GetMapping("/findAll")
    @ApiOperation("查询所有成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAll() {
        List<Achievements> achievements = achievementsService.findAll();
        return achievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), achievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("id") Long id) {
        Achievements achievements = achievementsService.findById(id);
        return achievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), achievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findById/{achievementsTypeId}")
    @ApiOperation("查询某类型的所有成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "achievementsTypeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByAchievementsTypeId(@PathVariable("achievementsTypeId") Long achievementsTypeId) {
        List<Achievements> achievements = achievementsService.findAllByAchievementsTypeId(achievementsTypeId);
        return achievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), achievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findAllByCatalogId/{catalogId}")
    @ApiOperation("查询目录下的所有成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "catalogId", value = "目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByCatalogId(@PathVariable("catalogId") Long catalogId) {
        List<Achievements> achievements = achievementsService.findAllByCatalogId(catalogId);
        return achievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), achievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @PostMapping("/findAllByCatalogIds")
    @ApiOperation("查询目录下(包含多个子目录)的所有成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "catalogIds", value = "目录ID列表", required = true, dataType = "array"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByCatalogIds(@RequestBody AchievementQueryParamDto achievementQueryParamDto) {
        List<Achievements> achievements = achievementsService.findAllByCatalogIds(achievementQueryParamDto.getCatalogIds());
        return achievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), achievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findAllByCatalogIdAndAchievementsTypeId/{catalogId}/{achievementsTypeId}")
    @ApiOperation("查询目录下的某类成果类型的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "catalogId", value = "目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "path", name = "achievementsTypeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByCatalogIdAndAchievementsTypeId(@PathVariable("catalogId") Long catalogId,@PathVariable("achievementsTypeId") Long achievementsTypeId) {
        List<Achievements> achievements = achievementsService.findAllByCatalogIdAndAchievementsTypeId(catalogId, achievementsTypeId);
        return achievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), achievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @PostMapping("/dataPresentation/findAllByCatalogIdAndAchievementsTypeIds")
    @ApiOperation("查询目录下(包含多个子目录)的某类成果类型的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "catalogIds", value = "目录ID列表", required = true, dataType = "array"),
            @ApiImplicitParam(paramType = "header", name = "achievementsTypeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByCatalogIdAndAchievementsTypeIds(@RequestBody AchievementQueryParamDto achievementQueryParamDto) {
        List<Achievements> achievements = achievementsService.findAllByCatalogIdAndAchievementsTypeIds(achievementQueryParamDto.getCatalogIds(), achievementQueryParamDto.getAchievementsTypeId());
        return achievements != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), achievements) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/dataPresentation/catalog/findAllCatalog")
    @ApiOperation("查询所有目录信息(树结构)-成果数据展示界面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllCatalog() {
        List<CatalogDto> catalogList = catalogService.findAll();
        return catalogList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), catalogList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findIServerAchievement")
    @ApiOperation("查询IServer的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findIServerAchievement() {
       return iServerService.queryLayerData();
    }

    @GetMapping("/findIServerService")
    @ApiOperation("查询IServer的服务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findIServerService() {
        return iServerService.queryServerData();
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @GetMapping("/findPages")
    @ApiOperation("分页查询矢量成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "isApproved", value = "0：审核不通过 1：审核通过 2：审核中", required = false, dataType = "int"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPages(Integer pageNumber, Integer pageSize, Integer isApproved) {
        return achievementsService.findPage(pageNumber, pageSize, isApproved);
    }

    @SystemLog(type = SystemLogEnum.SAVE_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping(value = "/save",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "新增成果数据", notes = "新增成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "content", value = "矢量数据（JSON对象）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "filePath", value = "文件路径", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "fileTye", value = "文件类型", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "所在目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "typeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "isApproved", value = "是否审核通过（0：审核不通过 1：审核通过 2：审核中）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "verifyType", value = "审核方式（0：系统审核 1：人工审核）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "uploadDate", value = "上传时间", required = true, dataType = "date"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(Achievements achievements, @ApiParam(name = "多文件", value = "multipartFile", required = true) MultipartHttpServletRequest multipartFiles, HttpServletRequest request) {
        MultiValueMap<String, MultipartFile> map = multipartFiles.getMultiFileMap();
        List<MultipartFile> list = map.get("multipartFile");// 获取到文件的列表
        return achievementsService.save(achievements,list,request);
    }

    @SystemLog(type = SystemLogEnum.UPDATE_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PutMapping(value = "/update",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "更新成果数据", notes = "更新成果数据（若上传文件则删除源文件）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "成果ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "content", value = "矢量数据（JSON对象）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "filePath", value = "文件路径", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "fileTye", value = "文件类型", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "所在目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "typeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "isApproved", value = "是否审核通过（0：审核不通过 1：审核通过 2：审核中）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "verifyType", value = "审核方式（0：系统审核 1：人工审核）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "uploadDate", value = "上传时间", required = true, dataType = "date"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState update(Achievements achievements,  @ApiParam(name = "多文件", value = "multipartFile", required = true) MultipartHttpServletRequest multipartFiles, HttpServletRequest request) {
        MultiValueMap<String, MultipartFile> map = multipartFiles.getMultiFileMap();
        List<MultipartFile> list = map.get("multipartFile");// 获取到文件的列表
        return achievementsService.update(achievements,list,request);
    }

    @SystemLog(type = SystemLogEnum.VERIFY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping(value = "/fill/save/{isLayerFeature}",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "兵团系统审核接口", notes = "兵团系统审核接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "content", value = "矢量数据（JSON对象）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "所在目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "typeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "isApproved", value = "是否审核通过（0：审核不通过 1：审核通过 2：审核中）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "verifyType", value = "审核方式（0：系统审核 1：人工审核）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "verifyContent", value = "系统审核内容", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "uploadDate", value = "上传时间", required = true, dataType = "date"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(@PathVariable("isLayerFeature") Integer isLayerFeature, Achievements achievements, @ApiParam(value = "multipartTextFile", required = true) MultipartFile multipartTextFile, @ApiParam(name = "多文件", value = "multipartVectorFiles", required = true) MultipartHttpServletRequest multipartVectorFiles, @ApiParam(value = "multipartEvidenceFile", required = true) MultipartHttpServletRequest multipartEvidenceFile, HttpServletRequest request) throws Exception {
        MultiValueMap<String, MultipartFile> mapVector = multipartVectorFiles.getMultiFileMap();
        List<MultipartFile> listVector = mapVector.get("multipartVectorFiles");// 获取到文件的列表
        MultiValueMap<String, MultipartFile> map = multipartEvidenceFile.getMultiFileMap();
        List<MultipartFile> listEvidence = map.get("multipartEvidenceFile");// 获取到文件的列表
        return achievementsService.save(isLayerFeature,achievements,multipartTextFile,listVector,listEvidence,request);
    }

    @SystemLog(type = SystemLogEnum.UPDATE_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PutMapping(value = "/fill/update/{isLayerFeature}",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "兵团系统审核接口-更新", notes = "兵团系统审核接口-更新")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "content", value = "矢量数据（JSON对象）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "所在目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "typeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "isApproved", value = "是否审核通过（0：审核不通过 1：审核通过 2：审核中）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "verifyType", value = "审核方式（0：系统审核 1：人工审核）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "verifyContent", value = "系统审核内容", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "uploadDate", value = "上传时间", required = true, dataType = "date"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState update(@PathVariable("isLayerFeature") Integer isLayerFeature, Achievements achievements, @ApiParam(value = "multipartTextFile", required = true) MultipartFile multipartTextFile, @ApiParam(name = "多文件", value = "multipartVectorFiles", required = true) MultipartHttpServletRequest multipartVectorFiles, @ApiParam(value = "multipartEvidenceFile", required = true) MultipartHttpServletRequest multipartEvidenceFile, HttpServletRequest request) throws Exception {
        MultiValueMap<String, MultipartFile> mapVector = multipartVectorFiles.getMultiFileMap();
        List<MultipartFile> listVector = mapVector.get("multipartVectorFiles");// 获取到文件的列表
        MultiValueMap<String, MultipartFile> map = multipartEvidenceFile.getMultiFileMap();
        List<MultipartFile> listEvidence = map.get("multipartEvidenceFile");// 获取到文件的列表
        return achievementsService.update(isLayerFeature,achievements,multipartTextFile,listVector,listEvidence,request);
    }

    @SystemLog(type = SystemLogEnum.VERIFY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping(value = "/fill/save/saveZipFile",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "批量导入矢量文件", notes = "批量导入矢量文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(Achievements achievements, @ApiParam(value = "multipartFileZip", required = true) MultipartFile multipartFileZip,@ApiParam(value = "multipartTextFile", required = true)MultipartFile multipartTextFile, HttpServletRequest request) throws Exception {
        return achievementsService.saveZipFile(achievements,multipartFileZip,multipartTextFile, request);
    }

    @SystemLog(type = SystemLogEnum.DELETE_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @DeleteMapping("/delete/{id}/{typeId}")
    @ApiOperation("根据ID删除成果数据信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "成果ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "path", name = "typeId", value = "成果类型ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState delete(@PathVariable("id") Long id,@PathVariable("typeId") Long typeId) {
        return achievementsService.delete(id,typeId);
    }

    @SystemLog(type = SystemLogEnum.VERIFY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping(value = "/verify/manualVerify/{isApproved}/{verifyContent}/{achievementId}")
    @ApiOperation(value = "兵团人工审核接口", notes = "兵团人工审核接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "isApproved", value = "是否审核通过（0：审核不通过 1：审核通过 2：审核中）", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "path", name = "verifyContent", value = "审核结果说明", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "path", name = "achievementId", value = "成果ID", required = true, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState manualVerify(@PathVariable("isApproved") Integer isApproved,@PathVariable("verifyContent") String verifyContent,@PathVariable("achievementId") Long achievementId,HttpServletRequest request) throws Exception {
        return achievementsService.manualVerify(isApproved, verifyContent, achievementId,request);
    }

    @PostMapping(value = "/fill/save/vectorConvert",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "矢量转换", notes = "矢量转换")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState vectorConvert(@ApiParam(name = "多文件", value = "multipartVectorFiles", required = true) MultipartHttpServletRequest multipartVectorFiles, HttpServletRequest request) throws Exception {
        MultiValueMap<String, MultipartFile> mapVector = multipartVectorFiles.getMultiFileMap();
        List<MultipartFile> listVector = mapVector.get("multipartVectorFiles");
        return achievementsService.vectorConvert(listVector, request);
    }
}
